<template>
  <!-- 商品采购列表 -->
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="70px">
      <el-form-item label="采购信息">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="采购商品/订单号"
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品供应商" label-width="95px">
        <el-select
          v-model="queryParams.supplierId"
          placeholder="请选择商品供应商"
          size="small"
          style="width: 160px"
        >
          <el-option label="请选择" value="-1" />
          <el-option
            :label="item1.supplierName"
            :value="item1.supplierId"
            v-for="(item1, index1) in supplierList"
            :key="index1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="margin-left: -15px">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 200px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAddOrder"
        >新增</el-button
      >
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
        >刷新</el-button
      >
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column
        label="序号"
        align="center"
        type="index"
        :show-overflow-tooltip="true"
        prop=""
      >
      </el-table-column>
      <el-table-column
        label="订单号"
        align="center"
        prop="orderNumber"
        width="186px"
      />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column
        label="采购价格"
        align="center"
        :show-overflow-tooltip="true"
        prop="procurePricing"
      >
      </el-table-column>
      <el-table-column label="采购数量" align="center" prop="procureNumber">
      </el-table-column>
      <el-table-column label="入库类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.stockType == 1 ? "公司" : "工厂" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.orderState == 0 ? "已完成" : "已撤销" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算方式" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.account == 1">微信</span>
          <span v-if="scope.row.account == 2">支付宝</span>
          <span v-if="scope.row.account == 3">现金</span>
          <span v-if="scope.row.account == 4">银行卡</span>
          <span v-if="scope.row.account == 5">其他</span>
        </template>
      </el-table-column>
      <el-table-column label="采购人" align="center" prop="procurer">
      </el-table-column>
      <el-table-column
        label="采购时间"
        align="center"
        prop="createTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="145"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleGoDetail(scope.row)"
            style="margin-right: 5px"
            >详情</el-button
          >
          <el-popconfirm
            v-if="scope.row.orderState == 0"
            @confirm="handleRevocation(scope.row)"
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确认撤销该条采购吗？"
          >
            <el-button
              slot="reference"
              size="mini"
              type="text"
              style="margin-left: 6px"
              >更多操作</el-button
            >
          </el-popconfirm>

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.orderState == 1"
            style="color: #ccc"
            >更多操作</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 采购详情 -->
    <el-dialog
      title="采购商品详情"
      :visible.sync="openDetail"
      width="600px"
      append-to-body
      class="dialog"
    >
      <el-form ref="form" :model="detailData" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单号：">
              <div>{{ detailData.orderNumber }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商：">
              <div>{{ detailData.supplierName }}</div>
            </el-form-item>
          </el-col>
               <el-col :span="12">
            <el-form-item label="商品名称：">
              <div>{{ detailData.goodsName }}</div>
            </el-form-item>
          </el-col>
    
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购价格：">
              <div>{{ detailData.procurePricing }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购数量：">
              <div>{{ detailData.procureNumber }}</div>
            </el-form-item>
          </el-col>
        </el-row>
                <el-row>
          <el-col :span="12">
            <el-form-item label="采购人 ：">
              {{ detailData.procurer }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购时间：">
              <div>{{ detailData.createTime }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态：">
              <div>{{ detailData.orderState == 0 ? "已完成" : "已撤销" }}</div>
            </el-form-item>
          </el-col>
                   <el-col :span="12">
            <el-form-item label="入库类型：">
              <div>{{ detailData.stockType == "1" ? "公司" : "工厂" }}</div>
            </el-form-item>
          </el-col>

        </el-row>

          <el-col :span="12">
            <el-form-item label="备注信息：">
              <div>{{ detailData.remarks }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算方式：">
              <span v-if="detailData.account == 1">微信</span>
              <span v-if="detailData.account == 2">支付宝</span>
              <span v-if="detailData.account == 3">现金</span>
              <span v-if="detailData.account == 4">银行卡</span>
              <span v-if="detailData.account == 5">其他</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 新建采购 -->
    <el-dialog
      title="新增采购"
      :visible.sync="open"
      width="900px"
      append-to-body
      @closed="cancel"
    >
      <div
        class="xian"
        style="
          height: 1px;
          width: 900px;
          background: #eee;
          margin-top: -26px;
          margin-left: -20px;
        "
      ></div>
      <el-form ref="form" :model="ruleForm" label-width="80px">
        <el-row style="margin-top: 15px">
          <el-col :span="9">
            <span class="title_" style="opacity: 0">1</span>
          </el-col>
          <el-col :span="10" style="display: flex; margin-top: -6px">
            <el-form-item
              label="供应商名称"
              label-width="100px"
              style="margin-left: 20%"
            >
              <el-select
                v-model="ruleForm.supplierId"
                placeholder="请选择供应商"
                size="small"
                style="width: 140px"
              >
                <el-option
                  :label="item1.supplierName"
                  :value="item1.supplierId"
                  v-for="(item1, index1) in supplierList"
                  :key="index1"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="结算方式">
              <el-select
                v-model="ruleForm.account"
                placeholder="请选择结算方式"
                size="small"
                style="width: 140px"
              >
                <el-option key="1" label="微信" value="1" />
                <el-option key="2" label="支付宝" value="2" />
                <el-option key="3" label="现金" value="3" />
                <el-option key="4" label="银行卡" value="4" />
                <el-option key="5" label="其他" value="5" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="max-height: 450px; overflow: auto">
          <el-col :span="24">
            <el-table
              v-loading="loading"
              :data="groupAddedList"
              style="width: 100%"
            >
              <el-table-column label="序号" align="center" type="index" />
              <el-table-column
                label="商品名称"
                align="center"
                width="160px"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <input
                    v-if="!scope.row.goodsId"
                    class="table_input"
                    type="text"
                    name=""
                    id=""
                    v-model="keyWord"
                    @input="handleInput"
                    style="
                      padding-left: 5px;
                      width: 130px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      border-radius: 4px;
                    "
                  />
                  <div class="moudel" v-show="keyWord && !scope.row.goodsId">
                    <div
                      class="row"
                      v-for="(item, index) in goodsList"
                      :key="index"
                      @click="handleChoose(index)"
                    >
                      <span>{{ item.goodsName }}</span
                      ><span class="choosed" v-show="item.isChoosed">已选</span>
                    </div>
                  </div>
                  <span v-if="scope.row.goodsName">{{
                    scope.row.goodsName
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="采购价"
                align="center"
                :show-overflow-tooltip="true"
                prop="procurePricing"
              >
                <template slot-scope="scope">
                  <input
                    v-if="scope.row.goodsId"
                    class="table_input"
                    type="number"
                    name=""
                    id=""
                    @blur="handleQueryT"
                    v-model="scope.row.procurePricing"
                    style="
                      width: 50px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      text-align: center;
                      border-radius: 4px;
                    "
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="采购数量"
                align="center"
                :show-overflow-tooltip="true"
                prop=""
              >
                <template slot-scope="scope">
                  <input
                    v-if="scope.row.goodsId"
                    class="table_input"
                    type="number"
                    name=""
                    id=""
                    @blur="handleQueryT"
                    v-model="scope.row.procureNumber"
                    style="
                      width: 50px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      border-radius: 4px;
                      text-align: center;
                    "
                  />
                </template>
              </el-table-column>
              <el-table-column label="单位" align="center" prop="unit" />

              <el-table-column
                label="入库仓库"
                align="center"
                :show-overflow-tooltip="true"
                prop=""
                width="100"
                height="60"
              >
                <template slot-scope="scope">
                  <el-select
                    v-if="scope.row.goodsId"
                    v-model="scope.row.stockType"
                    class="bottom_scope"
                    placeholder="选择"
                  >
                    <el-option value="">请选择</el-option>
                    <el-option :value="1" label="公司"></el-option>
                    <el-option :value="2" label="工厂"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="">
                <template slot-scope="scope">
                  <input
                    v-if="scope.row.goodsId"
                    v-model="scope.row.remarks"
                    class="table_input"
                    type="text"
                    name=""
                    id=""
                    style="
                      width: 65px;
                      outline: none;
                      border: 1px solid #dcdfe6;
                      text-align: center;
                      border-radius: 4px;
                    "
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="小计"
                align="center"
                :show-overflow-tooltip="true"
                prop=""
              >
                <template slot-scope="scope">
                  <span
                    v-if="scope.row.procureNumber && scope.row.procurePricing"
                    >{{
                      (
                        scope.row.procureNumber * scope.row.procurePricing
                      ).toFixed(2)
                    }}</span
                  >
                </template>
              </el-table-column>

              <el-table-column label="" align="center">
                <template slot-scope="scope">
                  <span
                    v-if="scope.row.procureNumber"
                    @click="handleDel(scope.$index, scope.row)"
                    style="color: #999; font-size: 14px; cursor: pointer"
                    >删除</span
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <div class="aggregate">总计：{{ totalPrice.toFixed(2) }}</div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">添加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  goodsProcureList,
  goodsSupplierList,
  goodsList,
  goodsProcureAdd,
  goodsProcureEdit,
} from "@/api/goods/index";
export default {
  data() {
    return {
      procurer: "",
      totalPrice: 0,
      noPath: false,
      groupAddedList: [{}], //模板列表
      goodsList: [],
      ruleForm: {
        supplierId: "",
        account: "",
      },
      supplierList: [],
      keyWord: "", //商品名称关键字
      loading: false,
      openDetail: false,
      detailData: {}, //详情
      dateRange: [], //日期参数
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: "-1",
        goodsName: null,
        beginTime: null,
        endTime: null,
      },
    };
  },
  created() {
    this.getList();
    goodsSupplierList({ pageNum: 1, pageSize: 20 }).then((res) => {
      if (res.code == 200) {
        this.supplierList = res.rows;
      }
    });
    this.procurer = localStorage.getItem("userName");
  },
  methods: {
    handleQueryT() {
      this.totalPrice = 0;
      this.groupAddedList
        .slice(0, this.groupAddedList.length - 1)
        .forEach((v) => {
          this.totalPrice += Number(v.procurePricing) * Number(v.procureNumber);
        });
    },
    handleInput() {
      //输入事件
      goodsList({ pageNum: 1, pageSize: 20, goodsName: this.keyWord }).then(
        (res) => {
          if (res.code == 200) {
            this.goodsList = res.rows.map((v) => {
              v.isChoosed = false;
              return v;
            });
            this.groupAddedList.forEach((v, i) => {
                this.goodsList.forEach((item) => {
                  if (item.goodsName == v.goodsName) {
                    item.isChoosed = true;
                  }
                });
            })
            // console.log(this.goodsList);
          }
        }
      );
    },
    handleChoose(index) {
      //选择哪个
      if (this.goodsList[index].isChoosed) {
        return false;
      }
      this.goodsList[index].isChoosed = true;
      this.groupAddedList.splice(this.groupAddedList.length - 1, 0, {
        goodsName: this.goodsList[index].goodsName,
        procurePricing: this.goodsList[index].procurePricing,
        goodsId: this.goodsList[index].goodsId,
        procureNumber: 1,
        stockType: 1,
        remarks: "",
        unit :this.goodsList[index].unit
      });
      this.keyWord = ""
      this.groupAddedList = [...new Set(this.groupAddedList)];
      this.totalPrice = 0;
      this.groupAddedList
        .slice(0, this.groupAddedList.length - 1)
        .forEach((v) => {
          this.totalPrice += Number(v.procurePricing) * Number(v.procureNumber);
        });
    },
    handleDel(index, row) {
      //删除事件
      this.groupAddedList.splice(index, 1);
      this.goodsList.forEach((v, i) => {
        if (v.goodsName == row.goodsName) {
          this.goodsList[i].isChoosed = false;
        }
      });
      this.totalPrice = 0;
      this.groupAddedList
        .slice(0, this.groupAddedList.length - 1)
        .forEach((v) => {
          this.totalPrice += Number(v.procurePricing) * Number(v.procureNumber);
        });
    },
    submitForm(formName) {
      this.noPath = false;
      if (!this.ruleForm.supplierId) {
        this.$message({
          message: "请选择供应商",
          type: "warning",
        });
        return false;
      }
      if (!this.ruleForm.account) {
        this.$message({
          message: "请选择结算方式",
          type: "warning",
        });
        return false;
      }
      if (this.groupAddedList.length <= 1) {
        this.$message({
          message: "请添加商品",
          type: "warning",
        });
        return false;
      }
      this.groupAddedList.forEach((v) => {
        if (v.procurePricing == "") {
          this.$message({
            message: "请输入商品价格",
            type: "warning",
          });
          this.noPath = true;
          return false;
        }
        if (v.procureNumber == "") {
          this.$message({
            message: "请输入商品数量",
            type: "warning",
          });
          this.noPath = true;
          return false;
        }
      });
      if (this.noPath) {
        return false;
      }
      this.groupAddedList.forEach((v) => {
        v.procureNumber = Number(v.procureNumber);
        v.procurePricing = Number(v.procurePricing);
      });

      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(
            this.groupAddedList.slice(0, this.groupAddedList.length - 1)
          );
          var procureJson = {
            account: Number(this.ruleForm.account),
            supplierId: Number(this.ruleForm.supplierId),
            totalAccount: this.totalPrice,
            procurer: this.procurer,
            data: this.groupAddedList.slice(0, this.groupAddedList.length - 1),
          };
          goodsProcureAdd({
            procureJson: JSON.stringify(procureJson),
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "添加采购成功",
                type: "success",
              });
              this.getList();
              this.groupAddedList = [{}];
              this.totalAccount = 0;
              this.ruleForm = {
                account: "",
                supplierId: "",
              };
              this.open = false;
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      //创建取消
      this.open = false;
      this.groupAddedList = [{}];
      this.totalAccount = 0;
      this.ruleForm = {
        account: "",
        supplierId: "",
      };
      this.keyWord = "";
    },
    handleGoDetail(row) {
      //查看详情
      this.openDetail = true;
      this.detailData = row;
    },
    handleClearCache() {
      window.location.reload();
    },
    handleRevocation(row) {
      //撤销
      var dataL = {
        procureId: row.procureId,
        orderState: 1,
        account: row.account,
        procurePricing: row.procurePricing,
        procureNumber: row.procureNumber,
        stockType: row.stockType,
      };
      goodsProcureEdit(dataL).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "撤销成功",
            type: "success",
          });
          this.getList();
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    handleAddOrder() {
      //新增采购
      this.open = true;
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      goodsProcureList(this.queryParams).then((response) => {
        this.typeList = response.rows.map((v) => {
          v.isMore = false;
          return v;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange.length >= 2) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      //重置事件
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        procureId: null,
        goodsName: null,
        remarks: null,
        beginTime: null,
        endTime: null,
        supplierId: "-1",
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog ::v-deep .el-form-item__label {
  text-align: justify !important;
  text-align-last: justify !important;
}
.aggregate {
  display: flex;
  justify-content: flex-end;
  margin-right: 20px;
  margin-top: 10px;
}
.moudel {
  // width: 300px;
  height: 150px;
  overflow: auto;
  background: #f7f7f7;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 13px;
  .row {
    cursor: pointer;
    margin-top: 13px;
    width: 116px;
    height: 25px;
    background: #ffffff;
    border-radius: 8px;
    display: flex;
    padding: 0 8px;
    box-sizing: border-box;
    justify-content: space-between;
    align-items: center;
    .choosed {
      width: 33px;
      height: 18px;
      background: rgba(133, 220, 204, 0.3);
      border-radius: 4px;
      text-align: center;
      line-height: 18px;
      font-size: 12px;
      font-family: Adobe Heiti Std;
      font-weight: normal;
      color: #07b087;
    }
  }
}
::v-deep .el-select__caret {
  line-height: 28px !important;
}
.bottom_scope ::v-deep .el-input__inner {
  outline: line !important;
  height: 28px !important;
  line-height: 28px !important;
  width: 80px !important;
}
.el-range-editor {
  width: 100% !important;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.app-container {
  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
    .create {
      margin-right: 20 px;
      float: right;
    }
  }
}
</style>